package crawler.http;

import constant.DBConstant;
import crawler.proxy.Proxy;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * USER: lintc 【lintiancong@zhuojianchina.com】
 * DATE: 2017-09-11 10:37
 */
public class HttpClientTest {

    public static final String URL = "https://movie.douban.com/subject/3604148/";

    public static void main(String[] args) {
        String url = DBConstant.DB_MOVIE_LIST_API.replace("MOVIE_TYPE", "40")
                .replace("LEVEL_START", "100")
                .replace("LEVEL_END", "90")
                .replace("START_NUM", "0")
                .replace("LIMIT_NUM", "20");
        String str = get(url);
        JSONArray ary = JSONArray.fromObject(str);
        if (ary.size() == 0) {
            System.out.println(00);
        }
        for (Object o : ary) {
            JSONObject obj = (JSONObject) o;
            System.out.println(obj);
        }

    }

    /**
     * 验证代理是否可用
     */
    public static boolean proxy(Proxy proxyIp, String url) {
        HttpGet get = new HttpGet(url);
        HttpHost proxy = new HttpHost(proxyIp.getIp(), Integer.parseInt(proxyIp.getPort())); // 设置代理 最好选择高匿
        get.setConfig(getRequestConfigBuilder().setProxy(proxy).build());

        CloseableHttpClient httpClient = getHttpClient();
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(get);
        } catch (IOException e) {
//            System.out.println(proxy + ":失败");
            return false;
        }
        try {
            if (response.getStatusLine().getStatusCode() != 200) {
                return false;
            }
            String str = EntityUtils.toString(response.getEntity(), Charset.defaultCharset());
            if (!str.contains("豆瓣")) {
                return false;
            }
//            System.out.println();
        } catch (IOException e) {
            return false;
        }
        System.out.println(proxy + ":成功");
        return true;
    }

    /**
     * 代理模式获取网页
     * @param proxyIp
     * @param url
     * @param charset
     * @return
     */
    public static String proxy2(Proxy proxyIp, String url, String charset) {
        HttpGet get = new HttpGet(url);
        get.setHeader("User-Agent", HttpUserAgent.getAgent());
        get.setHeader("Accept", "*/*");
        get.setHeader("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
        get.setHeader("Accept-Encoding", "gzip, deflate, br");
        get.setHeader("Referer", "https://movie.douban.com/");
//        get.setHeader("Cookie", "ll=\"118172\"; bid=4cSqvbMFYFE");
        HttpHost proxy = new HttpHost(proxyIp.getIp(), Integer.parseInt(proxyIp.getPort())); // 设置代理 最好选择高匿
        get.setConfig(getRequestConfigBuilder().setProxy(proxy).build());

        CloseableHttpClient httpClient = getHttpClient();
        CloseableHttpResponse response;
        String str = null;
        try {
            response = httpClient.execute(get);
        } catch (IOException e) {
//            System.out.println(proxy + ":失败 : " + e.getMessage());
            return null;
        }
        try {
            if (response.getStatusLine().getStatusCode() != 200) {
                return response.getStatusLine().getStatusCode() + "";
            }
//            System.out.println(proxy + ":成功");
            if (StringUtils.isBlank(charset)) {
                str = EntityUtils.toString(response.getEntity(), Charset.defaultCharset());
            } else {
                str = EntityUtils.toString(response.getEntity(), charset);
            }
            if (StringUtils.isNotBlank(str) &&
                    !str.contains("豆瓣")) {
                str = null;
            }
        } catch (IOException e) {
        }
        return str;
    }

    public static String get(String url) {
        String str = null;
        HttpGet get = new HttpGet(url);
        CloseableHttpClient httpClient = getHttpClient();
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(get);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            str = EntityUtils.toString(response.getEntity(), Charset.defaultCharset());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }

    public static void post(String url) {
        HttpPost http = new HttpPost(url);
        http.setHeader("User-Agent", HttpUserAgent.getAgent());
        http.setHeader("Accept", "*/*");
        http.setHeader("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
        http.setHeader("content-type", "application/x-www-form-urlencoded");
        http.setHeader("X-Requested-With", "XMLHttpRequest");
        http.setHeader("Cookie", "csrftoken=ZuqyqpEwKKuP2uTxjCW7pqGmoWPM61rT; UM_distinctid=15e7e1d413d51-0a089af823411a8-4c322c7f-1fa400-15e7e1d413e3d6; CNZZDATA1259490963=1306668351-1505354348-%7C1505354348");
        List<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>();
        pairList.add(new BasicNameValuePair("csrfmiddlewaretoken", "ZuqyqpEwKKuP2uTxjCW7pqGmoWPM61rT"));
        pairList.add(new BasicNameValuePair("curPage", "2"));
        pairList.add(new BasicNameValuePair("allPage", "10"));
        pairList.add(new BasicNameValuePair("pageType", "pageDown"));
        try {
            http.setEntity(new UrlEncodedFormEntity(pairList, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        CloseableHttpClient httpClient = getHttpClient();
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(http);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            System.out.println(EntityUtils.toString(response.getEntity(), Charset.defaultCharset()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static org.apache.http.client.config.RequestConfig.Builder getRequestConfigBuilder(){
        return RequestConfig.custom().
                setSocketTimeout(3000). // 指客户端从服务器读取数据的timeout
                setConnectTimeout(3000). // 指客户端和服务器建立连接的timeout
                setConnectionRequestTimeout(3000). // 指从连接池获取连接的timeout
                setCookieSpec(CookieSpecs.STANDARD);
    }

    public static CloseableHttpClient getHttpClient() {
        CloseableHttpClient httpClient;
        RequestConfig globalConfig = RequestConfig.custom()
                .setCookieSpec(CookieSpecs.DEFAULT).build();
        httpClient = HttpClients.custom().setDefaultRequestConfig(globalConfig)
                .build();
        return httpClient;
    }
}
